package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogInter;
import com.dlwx.parttimetalent.inter.GetAgeInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.LogUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.SexDialog;
import com.dlwx.parttimetalent.views.TimePublishDialog;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PublishInfoActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_title)
    EditText tvTitle;
    @BindView(R.id.tv_adress)
    TextView tvAdress;
    @BindView(R.id.ll_address)
    LinearLayout llAdress;
    @BindView(R.id.tv_label)
    TextView tvLabel;
    @BindView(R.id.tv_num)
    EditText tvNum;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_jz_right)
    TextView tvJzRight;
    @BindView(R.id.rl_jz)
    RelativeLayout rlJz;
    @BindView(R.id.et_jg)
    EditText etJg;
    @BindView(R.id.tv_time)
    TextView tvTime;
    private int sexStr;
    private String labelName, labelId;
    private boolean isDay=true;
    private String mintime;
    private LatLng latLng;
    private String cityStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_publish_info);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("发布任务/招聘");
        String address= (String) SPUtils.get(this,Constant.ADDRESS,"");
        cityStr= (String) SPUtils.get(this,Constant.CITY,"");
        tvAdress.setText(address);
    }

    @Override
    protected void onResume() {
        super.onResume();
        labelName = (String) SPUtils.get(this, Constant.labelName, "");
        labelId = (String) SPUtils.get(this, Constant.labelId, "");
        tvLabel.setText(labelName);
        SPUtils.put(this, Constant.labelName, "");
        SPUtils.put(this, Constant.labelId, "");
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_label, R.id.tv_sex, R.id.tv_confirm
            , R.id.ll_address,R.id.rl_jz,R.id.tv_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_label:
                startActivityForResult(new Intent(this, AllClassifyActivity.class), 0);
                break;
            case R.id.tv_sex:
                SexDialog dialog = new SexDialog(this, new GetAgeInter() {
                    @Override
                    public void getAge(int year, int month, int day, String start) {
                        tvSex.setText(start);
                        sexStr = day;
                    }
                });
                dialog.show();
                break;
            case R.id.tv_confirm:
                checkInputDatas();
                break;
            case R.id.rl_jz:
                if(isDay){
                    timetype="2";
                    tvJzRight.setText("小时");
                    isDay=false;
                }else{
                    timetype="1";
                    tvJzRight.setText("天");
                    isDay=true;
                }
                break;
            case R.id.tv_time:
                TimePublishDialog timeDialog;
                    timeDialog = new TimePublishDialog(this, true, new DialogInter() {
                        @Override
                        public void getInfo(String data) {
                            mintime = data.substring(0, data.length() - 1);
                            tvTime.setText(data);
                        }
                        @Override
                        public void getWorkInfo(String start, String end, String unit) {
                        }
                    });
                timeDialog.show();
                break;
            case R.id.ll_address:
                Intent i = new Intent(this, MapChooseActivity.class);
                startActivityForResult(i, 0);
                break;

        }
    }

    private void checkInputDatas() {
        String title = tvTitle.getText().toString();
        if (TextUtils.isEmpty(title)) {
            ToastUtils.toastForShort(this, "标题不能为空");
            return;
        }
        String address = tvAdress.getText().toString();
        if (TextUtils.isEmpty(address)) {
            ToastUtils.toastForShort(this, "任务/工作地址不能为空");
            return;
        }
        String num = tvNum.getText().toString();
        if (TextUtils.isEmpty(num)) {
            ToastUtils.toastForShort(this, "人数不能为空");
            return;
        }
        String label = tvLabel.getText().toString();
        if (TextUtils.isEmpty(label)) {
            ToastUtils.toastForShort(this, "通知不能为空");
            return;
        }
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.toastForShort(this, "工作内容简介不能为空");
            return;
        }
        String sex = tvSex.getText().toString();
        if (TextUtils.isEmpty(sex)) {
            ToastUtils.toastForShort(this, "性别不能为空");
            return;
        }
        String price = etJg.getText().toString();
        if (TextUtils.isEmpty(price)) {
            ToastUtils.toastForShort(this, "价格不能为空");
            return;
        }
        String time = tvTime.getText().toString();
        if (TextUtils.isEmpty(time)) {
            ToastUtils.toastForShort(this, "有效时间不能为空");
            return;
        }
        String longi = (String) SPUtils.get(this, Constant.LOCATION_LON, "116.395645");
        String lat = (String) SPUtils.get(this, Constant.LOCATION_LAT, "39.929985");
        try {
            double longDou=Double.parseDouble(longi);
            double latDou=Double.parseDouble(lat);
            latLng = new LatLng(latDou, longDou);
        }catch (Exception e){
            latLng=new LatLng(116.395645,39.929985);
            LogUtils.i(e.toString());
        }
        confrim(title, address, num, content, price, "", "", "");
    }
    String timetype="1";
    private void confrim(String title, String address, String num, String content, String price, String time, String timeStart, String timeEnd) {
        loadingDialog.show();
        OkgoUtil.post(Http.releasework, this)
                .params("worktitle", title)
                .params("workarea", address)
                .params("labelid", labelId)
                .params("number", num)
                .params("worksex", sexStr)
                .params("workdesc", content)
                .params("long", latLng.longitude)
                .params("lat", latLng.latitude)
                .params("yxtime", mintime)
                .params("timetype", timetype)
                .params("price", price)
                .params("city", cityStr)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(PublishInfoActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            PublishInfoActivity.this.finish();
                        } else {
                        }
                        ToastUtils.toastForShort(PublishInfoActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(PublishInfoActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case Constant.ONE:
                tvLabel.setText(data.getStringExtra("res"));
                break;
            case Constant.CITY_RESULT:
                tvAdress.setText(data.getStringExtra("address"));
                 latLng=data.getParcelableExtra("latlng");
                 cityStr=data.getStringExtra("city");
                break;
        }
    }
}
